// pages/makeAppointment/completeInfo/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    layout: [{
        label: '姓名：',
        placeholder: '请输入姓名',
        value: ''
      },
      {
        label: '性别：',
        isView: true,
        select: ['男', '女'],
        genderStatus: true,
      },
      {
        label: '年龄：',
        isView: true,
        select: [],
        genderStatus: false,
      },
      {
        label: '电话：',
        placeholder: '请输入电话',
        value: ''
      }
    ],
    genderIndex: 0, // 性别
    ageIndex: 5, // 年龄
    appointment: '立即预约',
    selectedCampus: '', // 已选校区
    selectProject: '', // 已选课程
    selectCampusId: '', // 校区ID
    seltctCurriculaId: [], // 已选课程ID
    baby_sex: 1, // 性别
    baby_age: 0, // 年龄

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initSelected(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this._initSelect()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 选择性别
  bindPickerChange(e) {
    this.setData({
      genderIndex: e.detail.value
    })
  },
  // 选择年龄
  bindPickerAgeChange(e) {
    this.setData({
      ageIndex: e.detail.value
    })
  },
  // 初始化年龄
  _initSelect() {
    const layout = 'layout[2].select'
    const arr = [...Array(100).keys()]
    this.setData({
      [layout]: arr
    })
  },
  // 初始化已选信息
  _initSelected(options) {
    const project = JSON.parse(options.project_name).join(',').replace(/\,/g, '，')
    this.setData({
      selectedCampus: options.campus_name,
      selectProject: project,
      selectCampusId: options.campus_id,
      seltctCurriculaId: JSON.parse(options.curricula_id)
    })
  },
  // 提交信息
  handlePostInfo() {
    const name = this.data.layout[0].value
    const phone = this.data.layout[3].value
    const gender = this.data.layout[1].select[this.data.genderIndex]
    const age = this.data.layout[2].select[this.data.ageIndex]
    const sex = gender === '男' ? 1 : 2
    const data = {
      campus_id: this.data.selectCampusId,
      campus_name: this.data.selectedCampus,
      curricula_id: this.data.seltctCurriculaId,
      baby_name: this.data.layout[0].value,
      baby_sex: sex,
      baby_age: age,
      parent_mobile: this.data.layout[3].value
    }
    if (name === '') {
      wx.showModal({
        title: '温馨提示',
        content: '请输入姓名',
        showCancel: false
      })
      return
    }
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入正确的手机号',
        showCancel: false
      })
      return;
    }
    app.request('profile/reserve', data, true).then((succ) => {
      wx.showModal({
        title: '预约成功',
        content: succ.msg,
        showCancel: false,
        success: () => {
          wx.switchTab({
            url: '../../profile/index',
          })
        }
      })
    }).catch((err) => {
      wx.showModal({
        title: '温馨提示',
        content: err.msg,
        showCancel: false
      })
    })
  },
  // 改变输入值
  handleValue(e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    const layout = `layout[${index}].value`
    this.setData({
      [layout]: value
    })
  }
})